<!--
 * @Description: 全部商品页面组件(包括全部商品,商品分类,商品搜索)
 * @Author: pdx
 * @Date: 2021-11-20 16:23:00
 -->
<template>
  <div class="goods" id="goods" name="goods">
    <!-- 面包屑 -->
    <div class="breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
        <el-breadcrumb-item>全部商品</el-breadcrumb-item>
        <el-breadcrumb-item v-if="search">搜索</el-breadcrumb-item>
        <el-breadcrumb-item v-else>分类</el-breadcrumb-item>
        <el-breadcrumb-item v-if="search">{{ search }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 面包屑END -->

    <!-- 分类标签 -->
    <div class="nav">
      <div class="product-nav">
        <div class="title">分类</div>
        <el-tabs v-model="activeName" type="card" @tab-click="getGameGoodsByCond">
          <el-tab-pane
              v-for="item in categoryList"
              :key="item.catId"
              :label="item.name"
              :name="''+item.catId"
          />
        </el-tabs>
      </div>
    </div>
    <!-- 分类标签END -->

    <!-- 主要内容区 -->
    <div class="main">
      <div class="list">
        <div>
          <el-row>
            <!-- 左侧列表 -->
            <el-col :span="20" v-if="gameList.length > 0">
              <div v-if="gameList != null">
                <div v-for="item in gameList" :key="item.id" class="div-list">
                  <router-link target="_blank" :to="{ path: '/game/details', query: {id:item.gameId} }">
                    <el-card shadow="hover">
                      <el-row>
                        <el-col :span="6">
                          <img class="image" :src="item.defaultImg">
                        </el-col>
                        <el-col :span="10">
                          <div>
                            <h3 style="margin-bottom: 5px; display: inline-block">{{ item.gameName }}</h3>
                            <el-tag v-if="item.seckillPrice != null" size="small" style="margin-left: 10px;background-color: red; color: white">促销</el-tag>
                          </div>

                          <p style="margin-top: 10px; font-size: 15px">发布于 {{ item.releaseTime }}</p>

                          <div style="margin-top: 28px">
                            <el-tag type="info" v-for="ele in item.gameTypeList" style="margin-right: 10px">{{ele.typeName}}</el-tag>
                          </div>
                        </el-col>
                        <el-col :span="8">
                          <div class="div-price">
                            <el-tag v-if="item.seckillPercentage != null" style="float: left; background-color: #ff6700; color: white" type="small">
                              -{{item.seckillPercentage}}%
                            </el-tag>
                            <span style="font-size: 0.8rem; color: #b0b0b0">
                              原价: ￥{{ item.price.toFixed(2) }}
                            </span>
                            <span v-if="item.seckillPrice != null"
                                  style="margin-left: 5px; font-size: 1rem; color: #ff6700;">
                              折后: ￥{{ item.seckillPrice.toFixed(2) }}
                            </span>
                          </div>
                        </el-col>
                      </el-row>
                    </el-card>
                  </router-link>
                </div>
              </div>
              <div v-else>
                抱歉没有找到相关的游戏商品，请看看其他的游戏
              </div>
            </el-col>
            <div v-else class="none-product">抱歉没有找到相关的游戏商品，请看看其他的游戏</div>
            <!-- 右侧条件 -->
            <el-col :span="4" style="float: right">
              <div class="div-left">
                <div class="div-type">
                  <h3>按类型</h3>
                  <el-checkbox-group v-model="checkGameTypes" @change="getCheckGameType">
                    <el-checkbox
                        v-for="item in gameTypeList"
                        :key="item.typeId"
                        :label="item.typeId"
                    >{{ item.typeName }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>

                <div class="div-price-search">
                  <h3>按价格</h3>
                  <div>
                    <el-radio-group v-model="searchPrice">
                      <el-radio @click.native.prevent="clickitem(1)" :label="1">￥0-10</el-radio>
                      <el-radio @click.native.prevent="clickitem(2)" :label="2">￥11-50</el-radio>
                      <el-radio @click.native.prevent="clickitem(3)" :label="3">￥51-100</el-radio>
                      <el-radio @click.native.prevent="clickitem(4)" :label="4">￥101-150</el-radio>
                      <el-radio @click.native.prevent="clickitem(5)" :label="5"> >=￥151</el-radio>
                    </el-radio-group>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      <!-- 分页 -->
      <el-pagination
          background
          :current-page="page"
          :page-size="limit"
          :total="total"
          style="padding: 30px 0; text-align: center;"
          layout="total, prev, pager, next, jumper"
          @current-change=""
      />
      </div>
      <!-- 分页END -->
    </div>
    <!-- 主要内容区END -->
  </div>
</template>
<script>
import gamesGoods from "@/api/game/gamesGoods";
import gameCategory from "@/api/game/gameCategory";
import gameType from "@/api/game/gameType";

export default {
  data() {
    return {
      categoryList: [
        {
          catId: 0,
          name: '全部'
        },
        {
          catId: 1,
          name: '畅销'
        },
        {
          catId: 2,
          name: '折扣'
        },
        {
          catId: 3,
          name: '最新'
        },
        {
          catId: 4,
          name: '评分'
        }
      ], //分类列表
      categoryID: [], // 分类id
      gameList: "",  //游戏列表
      activeName: 0, // 分类列表当前选中的id
      search: "", // 搜索条件
      page: 1,  //当前页码
      limit: 10,  // 每页显示的商品数量
      total: 0,  // 商品总量
      searchPrice: '',  //搜索价格
      gameTypeList: '', //游戏类型列表
      checkGameTypes: [],  //选中的游戏列表信息
      checkGameTypeStr: '',  //
    };
  },
  activated() {
    //获取分类
    this.getGameGoodsByCond()
    //获取游戏类型
    this.getGameType()
  },
  methods: {
    //处理tabs选中
    getGoodsByTabs() {
      gamesGoods.getGameGoodsByTabsInfo(this.page, this.activeName)
      .then(res => {
        const data = res.data
        if (data != null) {
          this.gameList = data.list
          this.total = data.totalCount
        } else {
          this.gameList = {}
        }
      }).catch(err => {
        this.gameList = {}
      })
    },

    //获取游戏类型
    getGameType() {
      gameType.getGameTypeInfo()
      .then(res => {
        this.gameTypeList = res.data
      })
    },

    //取消选中的单选框
    clickitem(e) {
      if (this.searchPrice === e) {
        this.searchPrice = ''
        this.getGameGoodsByCond()
        return
      }
      // 不是选中，选中当前点击 Radio
      this.searchPrice = e
      this.getGameGoodsByCond()
    },

    getCheckGameType() {
      this.checkGameTypeStr = this.checkGameTypes.join(',')
      this.getGameGoodsByCond()
    },

    getGameGoodsByCond() {
      gamesGoods.getGameGoodsInfoByCond(this.page, this.checkGameTypeStr, this.searchPrice, this.activeName)
      .then(res => {
        const data = res.data
        if (data != null) {
          this.gameList = data.list
        } else {
          this.gameList = ""
        }
      })
    }

  }
};
</script>

<style scoped>
.goods {
  background-color: #f5f5f5;
}

/* 面包屑CSS */
.el-tabs--card .el-tabs__header {
  border-bottom: none;
}

.goods .breadcrumb {
  height: 50px;
  background-color: white;
}

.goods .breadcrumb .el-breadcrumb {
  width: 1225px;
  line-height: 30px;
  font-size: 16px;
  margin: 0 auto;
}

/* 面包屑CSS END */

/* 分类标签CSS */
.goods .nav {
  background-color: white;
}

.goods .nav .product-nav {
  width: 1225px;
  height: 40px;
  line-height: 40px;
  margin: 0 auto;
}

.nav .product-nav .title {
  width: 50px;
  font-size: 16px;
  font-weight: 700;
  float: left;
}

/* 分类标签CSS END */

/* 主要内容区CSS */
.goods .main {
  margin: 0 auto;
  max-width: 1225px;
}

.goods .main .list {
  min-height: 650px;
  padding-top: 14.5px;
  margin-left: -13.7px;
  overflow: auto;
}

.goods .main .pagination {
  height: 50px;
  text-align: center;
}

.goods .main .none-product {
  color: #333;
  margin-left: 13.7px;
}

/* 主要内容区CSS END */

.div-list {
  width: 1000px;
  margin-top: 10px;
  margin-bottom: 10px;
}

.image {
  width: 10rem;
  height: 7rem;
  border-radius: 5px;
}

.div-price {
  margin-top: 40px;
  text-align: center;
}

.div-left {
  margin-left: 10px;
  margin-top: 10px;
}

.div-type {
  margin-top: 5px;
  margin-bottom: 5px;
}

.div-price-search {
  margin-top: 10px;
}

.el-radio {
  margin-top: 5px;
}
</style>